package com.ct.lotterysystem2.controller;

import com.ct.lotterysystem2.common.errorcode.ControllerErrorCodeConstants;
import com.ct.lotterysystem2.common.exception.ControllerException;
import com.ct.lotterysystem2.common.pojo.CommonResult;
import com.ct.lotterysystem2.common.utils.JacksonUtil;
import com.ct.lotterysystem2.controller.param.CreatePrizeParam;
import com.ct.lotterysystem2.controller.param.PageParam;
import com.ct.lotterysystem2.controller.result.FindPrizeListResult;
import com.ct.lotterysystem2.service.PictureService;
import com.ct.lotterysystem2.service.PrizeService;
import com.ct.lotterysystem2.service.dto.PageListDTO;
import com.ct.lotterysystem2.service.dto.PrizeDTO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.stream.Collectors;

@Slf4j
@RestController
public class PrizeController {

    @Autowired
    private PictureService pictureService;
    @Autowired
    private PrizeService prizeService;

    @RequestMapping("/pic/upload")
    public String uploadPicture(MultipartFile file) {
        return pictureService.savePicture(file);
    }


    // 表单数据
    @RequestMapping("/prize/create")
    public CommonResult<Long> createPrize (
            @Validated @RequestPart("param") CreatePrizeParam param,
            @RequestPart("prizePic") MultipartFile picFile ) {
        log.info("createPrize CreatePrizeParam:{}",
                JacksonUtil.writerValueAsString(param));
        return CommonResult.success(
                prizeService.createPrize(param, picFile));
    }

    @RequestMapping("/prize/find-list")
    public CommonResult<FindPrizeListResult> findPrizeList (PageParam param) {
        log.info("findPriceList PageParam:{}",
                JacksonUtil.writerValueAsString(param));
        PageListDTO<PrizeDTO> pageListDTO = prizeService.findPrizeList(param);
        return CommonResult.success(convertToFindPrizeListResult(pageListDTO));
    }

    private FindPrizeListResult convertToFindPrizeListResult(PageListDTO<PrizeDTO> pageListDTO) {
        if (null == pageListDTO) {
            throw new ControllerException(ControllerErrorCodeConstants.FIND_PRIZE_LIST_ERROR);
        }
        FindPrizeListResult result = new FindPrizeListResult();
        result.setTotal(pageListDTO.getTotal());
        result.setRecords(pageListDTO.getRecords()
                .stream()
                .map(prizeDTO -> {
                    FindPrizeListResult.PrizeInfo prizeInfo = new FindPrizeListResult.PrizeInfo();
                    prizeInfo.setPrizeId(prizeDTO.getPrizeId());
                    prizeInfo.setPrizeName(prizeDTO.getPrizeName());
                    prizeInfo.setDescription(prizeDTO.getDescription());
                    prizeInfo.setPrice(prizeDTO.getPrice());
                    prizeInfo.setImageUrl(prizeDTO.getImageUrl());
                    return prizeInfo;
                }).collect(Collectors.toList())
        );
        return result;
    }
}
